<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>序列化</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="language.enumerations.listing.html">« 枚举值清单</a></li>
      <li style="float: right;"><a href="language.enumerations.examples.html">范例 »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="language.enumerations.html">枚举</a></li>
    <li>序列化</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="language.enumerations.serialization" class="sect1">
  <h2 class="title">序列化</h2>

  <p class="para">
  枚举的序列化不同于对象。
  尤其是它们有新的序列化代码：
   <code class="literal">&quot;E&quot;</code>，指示了 enum 条目名称。
   然后反序列化动作能够设置变量为现有的单例值。
   确保那样：
  </p>

  <div class="example-contents">
   <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />Suit</span><span style="color: #007700">::</span><span style="color: #0000BB">Hearts&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">unserialize</span><span style="color: #007700">(</span><span style="color: #0000BB">serialize</span><span style="color: #007700">(</span><span style="color: #0000BB">Suit</span><span style="color: #007700">::</span><span style="color: #0000BB">Hearts</span><span style="color: #007700">));<br /><br />print&nbsp;</span><span style="color: #0000BB">serialize</span><span style="color: #007700">(</span><span style="color: #0000BB">Suit</span><span style="color: #007700">::</span><span style="color: #0000BB">Hearts</span><span style="color: #007700">);<br /></span><span style="color: #FF8000">//&nbsp;E:11:"Suit:Hearts";<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>


  <p class="para">
   如果枚举和它的条目在反序列化时，无法匹配序列化的值，
   会导致 warning 警告，并返回 <code class="literal">false</code>。
  </p>

  <p class="para">
   把纯粹枚举序列化为 JSON 将会导致错误。
   把回退枚举序列化为 JSON 时，仅会用标量值的形式，以合适的类型表达。
   可通过实现 <span class="classname"><a href="class.jsonserializable.html" class="classname">JsonSerializable</a></span> 来重载序列化行为。
  </p>

  <p class="para">对于 <span class="function"><a href="function.print-r.html" class="function">print_r()</a></span>，输出的枚举条目略微不同于对象，
  能减少迷惑。
  </p>

  <div class="example-contents">
   <div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />enum&nbsp;Foo&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">Bar</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">enum&nbsp;Baz</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">int&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;</span><span style="color: #0000BB">Beep&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">Foo</span><span style="color: #007700">::</span><span style="color: #0000BB">Bar</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">Baz</span><span style="color: #007700">::</span><span style="color: #0000BB">Beep</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;产生<br /><br />Foo&nbsp;Enum&nbsp;(<br />&nbsp;&nbsp;&nbsp;&nbsp;[name]&nbsp;=&gt;&nbsp;Bar<br />)<br />Baz&nbsp;Enum:int&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;[name]&nbsp;=&gt;&nbsp;Beep<br />&nbsp;&nbsp;&nbsp;&nbsp;[value]&nbsp;=&gt;&nbsp;5<br />}<br />*/<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
  </div>

 </div></div></div></body></html>